package com.smh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smh.common.CommonResult;
import com.smh.entity.Device;
import com.smh.mapper.DeviceMapper;
import com.smh.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;


@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public CommonResult<ArrayList<Device>> queryAll() {
        CommonResult<ArrayList<Device>> cr = new CommonResult<>();
        ArrayList<Device> list = null;
        try {
            list = deviceMapper.query();
            cr.setCode(0);
            cr.setCount(list.size());
            cr.setData(list);
            cr.setMsg("查询成功");
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            cr.setCode(-1);
            cr.setMsg("查询失败");
        }
        return cr;
    }

    @Override
    public CommonResult<Device> add(Device device) {
        CommonResult<Device> cr = new CommonResult<>();
        try {
            int add = deviceMapper.insert(device);
            cr.setCode(1);
            cr.setMsg("添加成功");
            cr.setData(device);
            cr.setCount(add);
            return cr;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            cr.setCode(-1);
            cr.setMsg("插入失败");
            return cr;
        }
    }

    @Override
    public CommonResult<Device> update(Device device) {
        CommonResult<Device> cr = new CommonResult<>();

        try {
            int update = deviceMapper.updateById(device);
            cr.setCode(1);
            cr.setMsg("修改成功");
            cr.setData(device);
            cr.setCount(update);
            return cr;
        } catch (Exception e) {
            cr.setCode(-1);
            cr.setMsg("修改失败");
            return cr;
        }
    }

    @Override
    public CommonResult<Device> delete(Integer id) {
        CommonResult<Device> cr = new CommonResult<>();
        try {
            int delete = deviceMapper.deleteById(id);
            cr.setCode(1);
            cr.setMsg("删除成功");
            cr.setCount(delete);
            return cr;
        } catch (Exception e) {
            cr.setCode(-1);
            cr.setMsg("删除失败");
            return cr;
        }
    }

    @Override
    public CommonResult<Device> queryById(Integer id) {
        CommonResult<Device> cr = new CommonResult<>();
        try {
            Device device = deviceMapper.selectById(id);
            cr.setCode(1);
            cr.setMsg("查询成功");
            cr.setCount(1);
            cr.setData(device);
            return cr;
        } catch (Exception e) {
            cr.setCode(-1);
            cr.setMsg("查询失败");
            return cr;
        }
    }

    @Override
    public CommonResult<ArrayList<Device>> listByPage(Integer page, Integer size) {
        CommonResult<ArrayList<Device>> cr = new CommonResult<>();
        try{
            //创建页对象
            Page<Device> pg = new Page<>(page,size);
            //查询包装器,将查询到的结果包装成Device对象
            QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
//          eq：等于，ne：不等于
//          gt：大于，ge：大于等于，lt：小于，le：小于等于
//          between：在值1和值2之间，notBetween：不在值1和值2之间
//          like：’%值%’，notLike：’%值%’，likeLeft：’%值’，likeRight：'值%'
//          isNull：字段 IS NULL，isNotNull：字段 IS NOT NULL
//          in：字段 IN (v0, v1, …)，notIn：字段 NOT IN (value.get(0), value.get(1), …)
//          or：或者 ，and：和
//          orderByAsc：升序：ORDER BY 字段, … ASC，orderByDesc：降序：ORDER BY 字段, … DESC
            deviceMapper.selectPage(pg, queryWrapper);
            //返回查询数据的列表
            ArrayList<Device> data = (ArrayList<Device>) pg.getRecords();
            long count = pg.getTotal();

            cr.setCode(0);
            cr.setMsg("查询成功");
            cr.setData(data);
            cr.setCount(count);
            return cr;
        }catch (Exception e){
            cr.setCode(-1);
            cr.setMsg("查询失败");
            return cr;
        }
    }
}
